package com.lv.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.tea.TeaException;
import com.lv.data.Result;
import com.lv.dto.SellerDto;
import com.lv.dto.UserDto;
import com.lv.enums.ResultEnum;
import com.lv.enums.ToKenEnum;
import com.lv.service.UserService;
import com.lv.util.JwtUtil;
import com.lv.util.LoginUser;
import com.lv.vo.UserVo;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService us;



    @Autowired
    private RocketMQTemplate mq;

    @Autowired
    private StringRedisTemplate redis;
    //注册
    @PostMapping
    public Result register(@RequestBody UserVo vo){
        //从 redis 中取出验证码   判断验证码是否正确
        String s = redis.opsForValue().get(vo.getPhone());
        if (!s.equals(vo.getPhoneNum())){
            return Result.response(ResultEnum.PHONENUM_ER);
        }
        us.register(vo);
        return Result.OK();
    }

    //发送短信
    @PostMapping("sendMessage")
    public Result sendMessage(@RequestParam String phoneNum){
        mq.convertAndSend("dk2206-topic",phoneNum);
        return Result.OK();
    }

    //登录
    @PostMapping("get")
    public Result<UserDto> getUserByName(@RequestParam("username") String username){
        UserVo vo = us.getUserByName(username);
        return Result.OK().setData(vo);
    }
}
